package org.dromara.app.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.dromara.app.appParams.AppVipTypeParams;
import org.dromara.app.cssUtils.CssUtils;
import org.dromara.app.domain.SkAppUser;
import org.dromara.app.domain.SkVipPrice;
import org.dromara.app.mapper.SkAppUserMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkMembershipOrderBo;
import org.dromara.app.domain.vo.SkMembershipOrderVo;
import org.dromara.app.domain.SkMembershipOrder;
import org.dromara.app.mapper.SkMembershipOrderMapper;
import org.dromara.app.service.ISkMembershipOrderService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 开会员订单Service业务层处理
 *
 * @author Evacuation
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class SkMembershipOrderServiceImpl implements ISkMembershipOrderService {

    private final SkMembershipOrderMapper baseMapper;

    private final SkAppUserMapper skAppUserMapper;

    /**
     * 查询开会员订单
     *
     * @param id 主键
     * @return 开会员订单
     */
    @Override
    public SkMembershipOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询开会员订单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 开会员订单分页列表
     */
    @Override
    public TableDataInfo<SkMembershipOrderVo> queryPageList(SkMembershipOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkMembershipOrder> lqw = buildQueryWrapper(bo);
        Page<SkMembershipOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的开会员订单列表
     *
     * @param bo 查询条件
     * @return 开会员订单列表
     */
    @Override
    public List<SkMembershipOrderVo> queryList(SkMembershipOrderBo bo) {
        LambdaQueryWrapper<SkMembershipOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkMembershipOrder> buildQueryWrapper(SkMembershipOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkMembershipOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SkMembershipOrder::getUserId, bo.getUserId());
        lqw.eq(bo.getVipType() != null, SkMembershipOrder::getVipType, bo.getVipType());
        lqw.eq(bo.getPrice() != null, SkMembershipOrder::getPrice, bo.getPrice());
        lqw.eq(bo.getVipLastTime() != null, SkMembershipOrder::getVipLastTime, bo.getVipLastTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSn()), SkMembershipOrder::getOrderSn, bo.getOrderSn());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), SkMembershipOrder::getCity, bo.getCity());
        lqw.eq(bo.getPayType() != null, SkMembershipOrder::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getPayTime()), SkMembershipOrder::getPayTime, bo.getPayTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPayOrderSn()), SkMembershipOrder::getPayOrderSn, bo.getPayOrderSn());
        lqw.eq(bo.getPayState() != null, SkMembershipOrder::getPayState, bo.getPayState());
        return lqw;
    }

    /**
     * 新增开会员订单
     *
     * @param bo 开会员订单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkMembershipOrderBo bo) {
        SkMembershipOrder add = MapstructUtils.convert(bo, SkMembershipOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改开会员订单
     *
     * @param bo 开会员订单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkMembershipOrderBo bo) {
        SkMembershipOrder update = MapstructUtils.convert(bo, SkMembershipOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkMembershipOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除开会员订单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 生成订单数据
     * @param price
     * @param skAppUser
     * @param params
     * @return
     */
    @Override
    public SkMembershipOrder createOrder(SkVipPrice price, SkAppUser skAppUser, AppVipTypeParams params) {
        SkMembershipOrder sk = new SkMembershipOrder();
        sk.setPrice(price.getPrice());
        sk.setUserId(skAppUser.getId());
        sk.setCity(skAppUser.getCity());
        sk.setPayState(0L);
        sk.setPayType(params.getPayType());
        sk.setVipType(price.getType());
        sk.setOrderSn("VIP"+ CssUtils.generateOrderNumber());
        baseMapper.insert(sk);
        return sk;
    }

    /**
     * 支付成功 更新订单信息
     * @param skMembershipOrder
     */
    @Override
    public void updateByInfo(SkMembershipOrder skMembershipOrder) {
        skMembershipOrder.setPayState(1L);
        skMembershipOrder.setVipLastTime(DateUtil.offset(new Date(), DateField.MONTH,1));
        baseMapper.updateById(skMembershipOrder);
        // 更新用户会员信息
        SkAppUser skAppUser = skAppUserMapper.selectById(skMembershipOrder.getUserId());
        skAppUser.setVipType(skMembershipOrder.getVipType());
        skAppUser.setVipPayTime(new Date());
        skAppUser.setVipTime(DateUtil.format(skMembershipOrder.getVipLastTime(), DatePattern.NORM_DATE_PATTERN));
        skAppUserMapper.updateById(skAppUser);

    }

    /**
     * 根据订单号查询订单信息
     * @param outTradeNo
     * @return
     */
    @Override
    public SkMembershipOrder getOrderById(String outTradeNo) {
        return baseMapper.selectById(outTradeNo);
    }
}
